const app = getApp();
//var md5 = require('../utils/newMd5.js');
var md5 = require('md5.js');
var apiHost = "http://localhost:5003/";
var tokenKey = "token";
// 登录地址, 根据这个地址来设置token
var logInUrl = "/Account/LogInForMiniProgram";
// 例外不用token的地址
var exceptionAddrArr = [
  'http://localhost:8080/',
];

function loginRequest(url, data, success, fail) {
 var header= {
   "Content-Type": 'json'
  };
  wx.request({
    url: exceptionAddrArr + url,
    method: 'GET',
    data: data,
    header: header,
    success: function (res) {
      console.log("get成功")
      console.log(res)
    },
    fail: function (error) {
      console.log("get没成功")
      console.log(res)
    }
  })
}


/** 
 * get请求
 */
function getRequest(url, data, success, fail) {
  var header = CreateHeader();
  wx.request({
    url: exceptionAddrArr + url,
    method: 'GET',
    data: data,
    header: header,
    success: function (res) {
      return res;
    },
    fail: function (error) {
      return error
    }
  })
}
/** 
 *post请求
 */
function postRequest(url, dataresult, success, fail) {
  var header = CreateHeader();
  wx.request({
    url: exceptionAddrArr+url,
    method: 'post',
    data: dataresult,
    header: header,
    success: function (res) {
     return res;
    },
    fail: function (error) {
      return error
    }
  })
}

/** 
 *  创建头部token
 */
function CreateHeader() {
  var id = app.globalData.userInfo.uuid;  //用户标识 
  var date = GetTime();
  var curr = id + "yunhuan" + date + "yhsft";
  console.log(curr)
  console.log(md5.hexMD5(curr))
 var header={
   'content-type': 'application/json' ,
    'X-Token' : id,
   'User-Token': md5.hexMD5(curr),
  }
    return header;
}
/**时间年月日 */
function GetTime() {
  var data = '';
  var dataTimes = new Date();
  //获取年月日
  var dataYear = dataTimes.getFullYear();
  var dataMonth = dataTimes.getMonth() + 1;
  var dataDay = dataTimes.getDate();    
  var dataHours = dataTimes.getHours();
  if (dataMonth < 10) {
    dataMonth = '0' + dataMonth;
  };
  if (dataDay < 10) {
    dataDay = '0' + dataDay;
  };
  if (dataHours < 10) {
    dataHours = '0' + dataHours;
  };
  data = dataYear + '' + dataMonth + '' + dataDay + '' + dataHours
  console.log(data)
  return data;
}
module.exports.CreateHeader = CreateHeader;
module.exports.getRequest = getRequest;
module.exports.postRequest = postRequest;